﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.IBUS;
using System.Data;
using SD.IDAL;
using SD.Entities;
using SD.DAL;
using System.Data.SqlClient;

namespace SD.BUS
{
    public class ReferenceDataBIZ : BaseBIZ,IReferenceDataBIZ
    {
        #region IReferenceDataBIZ Members

        /// <summary>
        /// Get all reference add into list reference
        /// </summary>
        /// <returns>list reference</returns>
        public List<ReferenceData> GetAll()
        {
            List<ReferenceData> lstResult = new List<ReferenceData>();

            try
            {
                IReferenceDataDAO iReferenceDataDAO = new ReferenceDataDAO();
                lstResult = iReferenceDataDAO.GetAll();
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return lstResult;
        }

        /// <summary>
        /// Get all reference by refCode
        /// </summary>
        /// <param name="refCode"></param>
        /// <returns>list reference</returns>
        public List<ReferenceData> GetByRefCode(string refCode)
        {
            List<ReferenceData> lstResult = new List<ReferenceData>();

            try
            {
                IReferenceDataDAO iReferenceDataDAO = new ReferenceDataDAO();
                lstResult = iReferenceDataDAO.GetByRefCode(refCode);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return lstResult;
        }

        /// <summary>
        /// Get detail reference by refId
        /// </summary>
        /// <param name="refId"></param>
        /// <returns>Reference</returns>
        public ReferenceData GetById(string refId)
        {
            IReferenceDataDAO refDAO = new ReferenceDataDAO();
            try
            {
                return refDAO.GetById(refId);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        
        /// <summary>
        /// Process exception for event insert
        /// </summary>
        /// <param name="refData"></param>
        /// <returns>true or false</returns>
        public bool Insert(ReferenceData refData)
        {
            return false;
        }

        /// <summary>
        /// Process exception for event update
        /// </summary>
        /// <param name="refData"></param>
        /// <returns>true or false</returns>
        public bool Update(ReferenceData refData)
        {
            return false;
        }

        /// <summary>
        /// Process exception for event delete
        /// </summary>
        /// <param name="refId"></param>
        /// <returns>true or false</returns>
        public bool Delete(string refId)
        {
            return false;
        }

        public List<ReferenceData> GetRefByCodition(string bussinessname, string siccode)
        {
            List<ReferenceData> lstResult = new List<ReferenceData>();

            try
            {
                IReferenceDataDAO iReferenceDataDAO = new ReferenceDataDAO();
                lstResult = iReferenceDataDAO.GetRefByCodition(bussinessname, siccode);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return lstResult;
        }

        /// <summary>
        /// Get all reference add into data table
        /// </summary>
        /// <returns>data table</returns>
        public DataTable ListAll()
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("RefID");
            dtbResult.Columns.Add("RefCode");
            dtbResult.Columns.Add("RefValue");

            try
            {
                List<ReferenceData> lstReferenceData = GetAll();
                //If rows of list > 0
                if (lstReferenceData.Count > 0)
                {
                    //Get reference from list reference
                    foreach (ReferenceData referenceData in lstReferenceData)
                    {
                        dtbResult.Rows.Add(referenceData.RefId, referenceData.RefCode, referenceData.RefValue);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }

        /// <summary>
        /// Get List ReferecnceData by Refcode
        /// </summary>
        /// <param name="refCode"></param>
        /// <returns>Data table</returns>
        public DataTable ListReferenceDataByRefcode(string refCode)
        {
            IReferenceDataDAO iRefDao = new ReferenceDataDAO();
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("RefID");
            dtResult.Columns.Add("RefCode");
            dtResult.Columns.Add("RefValue");
            try
            {
                List<ReferenceData> listRef = iRefDao.GetByRefCode(refCode);
                //If rows of list >0
                if (listRef.Count > 0)
                {
                    //Get reference from list reference
                    foreach (ReferenceData reData in listRef)
                    {
                        dtResult.Rows.Add(reData.RefId, reData.RefCode, reData.RefValue);
                    }
                }

            }
            catch (SqlException ex)
            {

                throw ex;
            }
            return dtResult;
        }
           
        /// <summary>
        /// Get all reference by conditions
        /// </summary>
        /// <param name="bussiness"></param>
        /// <param name="siccode"></param>
        /// <returns>data tale</returns>
        public DataTable ListRefByCodition(string bussiness, string siccode)
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("RefID");
            dtbResult.Columns.Add("RefCode");
            dtbResult.Columns.Add("RefValue");

            try
            {
                List<ReferenceData> lstReferenceData = GetRefByCodition(bussiness, siccode);
                //If rows of list >0
                if (lstReferenceData.Count > 0)
                {
                    //get reference from list reference
                    foreach (ReferenceData referenceData in lstReferenceData)
                    {
                        dtbResult.Rows.Add(referenceData.RefId, referenceData.RefCode, referenceData.RefValue);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }
        #endregion
    }
}
